﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace SkinableFormsLib
{
    public class LWButton : Button, ILWControl
    {
        Graphics _lwGraphics;
        Bitmap _lwBitmap;

        public LWButton()
        {
            _lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this._lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            _lwGraphics = Graphics.FromImage(_lwBitmap);
        }


        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //base.OnPaintBackground(pevent);
        }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            if (DesignMode)
            {
                base.OnPaint(pevent);
                return;
            }
            if (_lwTransparent && BackgroundImage != null)
            {

                this._lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                _lwGraphics = Graphics.FromImage(_lwBitmap);

                switch (this.BackgroundImageLayout)
                {
                    case ImageLayout.Center:
                        DrawCenter();
                        break;
                    case ImageLayout.None:
                        DrawCenter();
                        break;
                    case ImageLayout.Stretch:
                        DrawStretch();
                        break;
                    case ImageLayout.Tile:
                        DrawCenter();
                        break;
                    case ImageLayout.Zoom:
                        DrawCenter();
                        break;
                }
            }
            else
            {
                PaintEventArgs peventx = new PaintEventArgs(Graphics.FromImage(_lwBitmap), pevent.ClipRectangle);
                base.OnPaint(peventx);
            }


        }


        bool _lwMouseDown = false;
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            _lwMouseDown = true;
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            _lwMouseDown = false;

        }
        private int _lwPressedShift = 1;
        private void DrawCenter()
        {
            if (_lwMouseDown)
                _lwGraphics.DrawImage(BackgroundImage, _lwPressedShift + (Width - BackgroundImage.Width) / 2, _lwPressedShift + (Height - BackgroundImage.Height) / 2, BackgroundImage.Width, BackgroundImage.Height);
            else
                _lwGraphics.DrawImage(BackgroundImage, (Width - BackgroundImage.Width) / 2, (Height - BackgroundImage.Height) / 2, BackgroundImage.Width, BackgroundImage.Height);
        }

        private void DrawStretch()
        {
            if (_lwMouseDown)
                _lwGraphics.DrawImage(BackgroundImage, _lwPressedShift, _lwPressedShift, Width - _lwPressedShift, Height - _lwPressedShift);
            else
                _lwGraphics.DrawImage(BackgroundImage, 0, 0, Width - _lwPressedShift, Height - _lwPressedShift);

        }

        protected override void WndProc(ref Message m)
        {

            if (m.Msg == Win32.WM_LBUTTONDOWN || m.Msg == Win32.WM_LBUTTONDBLCLK)
            {

                //Int32 pos=System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam);
                Int32 x;
                Int32 y;

                y = Utils.SignedHIWORD(m.LParam);
                x = Utils.SignedLOWORD(m.LParam);



                Point p = new Point(Left + x, Top + y);
                //BitmapData bd=_lwBitmap.LockBits(new Rectangle(x, y, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                //if (System.Runtime.InteropServices.Marshal.ReadByte(bd.Scan0, 0) > 0)
                //if (x>=0 && y>=0 && _lwBitmap.Width>=x && _lwBitmap.Height>=y && _lwBitmap.GetPixel(x, y).A > 0)
                this.GetType();
                if (_lwBitmap.GetPixel(x, y).A > 0)
                {
                    //    _lwBitmap.UnlockBits(bd);
                    base.WndProc(ref m);
                    return;
                }
                else
                {
                    /*if (false && Parent is LWSkinableForm)
                     {
                         //((LWSkinableForm)Parent).DoMouseDown(this)
                         //Console.WriteLine(this.Parent.GetChildAtPoint(new Point(Left + x, Top + y)));
                         //Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(c, c.Handle), Win32.WM_LBUTTONDOWN, (IntPtr)Win32.MKS.MK_LBUTTON, Utils.MAKELPARAM(-c.Left + Left + x, -c.Top + Top + y));
                     }
                     else
                     {*/

                    bool foundThis = false;
                    bool clickSent = false;
                    foreach (Control c in this.Parent.Controls)
                    {
                        Console.WriteLine(c);
                        if (c.Equals(this))
                        {
                            foundThis = true;
                            Console.WriteLine("FOUND THIS");
                        }
                        else
                            if (foundThis && c.Bounds.Contains(p))
                            {
                                Console.WriteLine("SEND CLICK DOWN {0}", c);
                                Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(c, c.Handle), Win32.WM_LBUTTONDOWN, (IntPtr)Win32.MKS.MK_LBUTTON, Utils.MAKELPARAM(-c.Left + Left + x, -c.Top + Top + y));
                                clickSent = true;
                                break;
                            }
                    }
                    if (!clickSent)
                        Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(Parent, Parent.Handle), Win32.WM_NCHITTEST, (IntPtr)Win32.MKS.MK_LBUTTON, Utils.MAKELPARAM(Left + x, Top + y));
                    /*}*/
                    //  _lwBitmap.UnlockBits(bd);
                    return;
                }


            }

            base.WndProc(ref m);
        }



        protected override void DefWndProc(ref Message m)
        {
            base.DefWndProc(ref m);
        }




















        #region ILWControl Members

        System.Drawing.Bitmap ILWControl.LWBitmap
        {
            get
            {
                /*    if (this.Image != null)
                    {
                        //this.BackgroundImageLayout==ImageLayout.Stretch
                        //if (this.BackgroundImage!=null)                    return (Bitmap)this.BackgroundImage;
                        //_lwBitmap = (Bitmap)this.Image;
                        _lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        Graphics g = Graphics.FromImage(_lwBitmap);

                        g.DrawImage(Image, 0, 0, Image.Width, Image.Height); 
                        Console.WriteLine("-- {0} {1}",this.Image.Width, _lwBitmap.PhysicalDimension);

                        //return (Bitmap)this.Image;
                    }*/
                return _lwBitmap;
            }
        }
        #endregion

        public override Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {
                base.BackgroundImage = value;
            }
        }



        #region ILWControl Members

        private bool _lwTransparent = false;
        public bool LWTransparent
        {
            get
            {
                return _lwTransparent;
            }
            set
            {
                _lwTransparent = value;
            }
        }


        #endregion
    }
}
